<?php

namespace Home\Controller;

use Admin\Controller\CommonController;
use Think\Controller;

class PersonalController extends CommoController
{
    public function addSite()
    {

        //  添加地址

        $model_add_site = D('user');
        $site = $_POST['site'];
        $member = session('member');
        $member_id = $member[0]['id'];
        if ($model_add_site->where("id = '$member_id'")->setField('site', "$site")) {
            $this->ajaxRet(self::RET_FAILED, '添加成功');
        } else {
            $this->ajaxRet(self::RET_SUCCESS, '添加失败');
        }
    }


    //    验证邮箱

    public function checkEmail()
    {
        $email = $_POST['email'];
        $model_add_email = D('user');
        $sql = sprintf("SELECT * FROM user WHERE email ='%s'", $email);
        $res = $model_add_email->query($sql);
        $code = !$res ? self::RET_FAILED : self::RET_SUCCESS;
        $msg = !$res ? '可用' : '不可用';
        $this->ajaxRet($code, $msg);
    }


    //  添加邮箱

    public function addEmail()
    {
        $model_add_email = D('user');
        $email = $_POST['email'];
        $member = session('member');
        $member_id = $member[0]['id'];
        if ($model_add_email->where("id = '$member_id'")->setField('email', "$email")) {
            $this->ajaxRet(self::RET_FAILED, '添加成功');
        } else {
            $this->ajaxRet(self::RET_SUCCESS, '添加失败');
        }
    }


    //  验证旧密码

    public function checkPaw()
    {
        $data['password'] = $_POST['password'];
        $model_check_paw = D('user');
        $member = session('member');
        $data['id'] = $member[0]['id'];
        $rule = where_model()->parseWhere($data);
        $res = $model_check_paw->query("SELECT * FROM user WHERE $rule");
        $code = !$res ? self::RET_FAILED : self::RET_SUCCESS;
        $msg = !$res ? '可用' : '不可用';
        $this->ajaxRet($code, $msg);
    }


    //  修改密码

    public function upPaw()
    {
        $model_up_paw = D('user');
        $password = $_POST['new_confirm_password'];
        $member = session('member');
        $member_id = $member[0]['id'];
        if ($model_up_paw->where("id = '$member_id'")->setField('password', "$password")) {
            $this->ajaxRet(self::RET_FAILED, '修改成功');
        } else {
            $this->ajaxRet(self::RET_SUCCESS, '修改失败');
        }
    }


    //  上传头像

    public function file()
    {
        if ($_FILES['file']['tmp_name'] == '') {
            $this->ajaxRet(self::RET_FAILED, '上传失败');
        }
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'image', 'JPEG');// 设置附件上传类型
        $upload->rootPath = $_SERVER['DOCUMENT_ROOT']; // 设置附件上传根目录
        $upload->savePath = '/Public/Portarait/'; // 设置附件上传（子）目录
        // 上传单个文件
        $info = $upload->uploadOne($_FILES['file']);
        if (!$info) {
            $this->ajaxRet(self::RET_FAILED, $upload->getError());
        }
        $fileName = $info['savepath'] . $info['savename'];// 上传成功 获取上传文件信息

        $this->ajaxRet(self::RET_SUCCESS, "上传成功", ["fileName" => $fileName]);


    }


    //  保存头像

    public function addPic()
    {
        $fileName = $_POST['pic'];
        $model_add_pic = D('user');

        $member = session('member');
        $member_id = $member[0]['id'];
        if ($model_add_pic->where("id = '$member_id'")->setField('pic', "$fileName")) {
            $this->ajaxRet(self::RET_FAILED, '保存成功');
        } else {
            $this->ajaxRet(self::RET_SUCCESS, '保存失败');
        }
    }


    //  退出登录

    public function out()
    {
        session('member', null);
        $this->success('退出成功', U('Login/login'));
    }

}